package com.kiln.juxingqs.ui

import android.content.Intent
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import client.global.commlib.toast.toast
import client.global.commlib.utils.StoreUtil
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.api.bean.UserInfo
import com.kiln.juxingqs.databinding.ActivityZrxyBinding
import com.kiln.juxingqs.dialog.JyInputNumDialog
import com.kiln.juxingqs.dialog.PagingFixedBathDialog
import com.kiln.juxingqs.ui.vm.ZrxyViewModel

class ZrXyActivity : BaseMiddleActivity<ZrxyViewModel, ActivityZrxyBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_zrxy
    }

    val userInfo = MutableLiveData<UserInfo>().apply {
        value = StoreUtil.getBean<UserInfo>(this@ZrXyActivity)
    }

    var goodsId: String? = null

    override fun initView() {
        binding?.apply {
            event = this@ZrXyActivity
            vm = viewModel
        }
        var type = intent.getIntExtra("type", -1)
        var jy_price = intent.getStringExtra("jy_price")
        goodsId = intent.getStringExtra("goods_id")
        if (type == 1) {
            binding.tvTitle.text = "冻结转让"
            binding.etPwdOld.setText(jy_price)
            viewModel.price.value = jy_price
        } else {
            binding.etPwdOld.setText(userInfo.value!!.transfer_price)
            viewModel.price.value = userInfo.value!!.transfer_price
        }
        goodsId?.let {
            viewModel.goodsId.value = it
        }

    }

    override fun observerUI() {
        viewModel.call.observe(this, Observer {
            it?.apply {
                if (isSuccess) {
                    toast("操作成功")
                    startActivity(Intent(this@ZrXyActivity, XyzrActivity::class.java).apply {
                        putExtra("index", 2)
                    })
                    finish()
                } else {
                    toast(it.message, it.code)
                }
            }
        })

        viewModel.otherAccount.observe(this, {
            viewModel.otherName.value = ""
        })
        viewModel.queryRet.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    viewModel.otherName.value = it.data?.real_name
                } else {
                    toast(it.message)
                }
            }
        })
    }

    fun selectNum() {
        PagingFixedBathDialog(this@ZrXyActivity).apply {
            vm = viewModel
            ok = {
                viewModel.selectBath.value = it
                JyInputNumDialog(this@ZrXyActivity).apply {
                    ok = { num ->
                        viewModel.sNum.value = "${num}"
                        dismiss()
                    }
                }.show()
            }
        }.show()
    }

    override fun initViewModel(): ZrxyViewModel = createViewModel()

    fun lsjl() {
        startActivity(Intent(this, XyzrActivity::class.java))
    }
}